ITで遊ぶ

カラーミー 出荷日のリスト

新しい税理士さんと話しをしていて、「売上の立つ日はいつか?」という原則論になりました。

一般的には「「実現主義」と呼ばれる原則が用いられています。

物販では「出荷した日」なのです。
これ税務の話なんで、テキトーにできないらしいです。

ところがカラーミーのいろんな例を見ても、ほとんどが入金日を売上げの立つ日にしているようです。
この現金主義はあくまで「特例」で、小規模事業者(後述)に限り「現金主義の所得計算による旨の届出手続(青色申告者あるいは同時に青色申告承認申請をする場合に限る)」をしている場合に限ります。

小規模事業者とは、その年の前々年分の事業所得の金額の合計額が300万円以下である方のことをいうそうで、ちょっとした会社だともう無理。

逆に言えば、カラーミーで「儲かってます」とか言っても年商300万円もいっていないところがほとんだということではないだろうか?これはビックリ。おまえら食えてるの?

去年まで私は某会計事務所と経理やってた人に騙されていたわけです。もし税務署の査察が入ったら当然、指摘されるわけで。悲しすぎる。

で、カラーミーのどこを探しても出荷日(メールを出した日ですが)を出してくれるレポートがないので、イヤイヤ作ることにしました。
最近のカラーミーは宣伝しまくってて資金回収をしたいのでしょう、オプションですぐカネを取ろうとするので、今年中にはShopifyに変更したいものです。
以下、私の書いたコード。(日本語注釈に注意)

ロジックを簡単に説明すると、set_up関数でoauthという認証をパスします。次に対象となる売上げIDをゲットします。(request_sales)。そして各々の売上についてデータを入手します(request_detail)。
簡単です。

コードにヘンなpre,codeタグが入るのはWordpressが勝手に入れるのでごめん。


<?php
// global 変数
$AFTER = "2020-04-01"; //after this date ここは一年に一回、決算日で変更してね。
$TOTAL = 0; // total sales record
$OFFSET = 0; // sales offset

// カラーミー認証
function set_up($code){
$request_options = array(
'http' => array(
'method' => 'GET',
'header'=> 'Authorization: Bearer '.$code."\r\n",
'ignore_errors' => true, // i want to get http status code.
)
);
$context = stream_context_create($request_options);
return $context;
}

// IDを集める
// sent
function request_sales($context, $after,$offset){
global $TOTAL;
global $OFFSET;
$url = 'https://api.shop-pro.jp/v1/sales.json';
$request = '?delivered=true&paid=true&delivered_mail_stat=sent&fields=id&limit=100&offset='.$offset.'&after='.$after;
$response_body = file_get_contents($url.$request, false, $context);
$pos = strpos($http_response_header[0],'200');
if ($pos === false ){ // usually don't come here.
print_r($response_body);
return NULL;
}
$response_json = json_decode($response_body, true);// associated array
$TOTAL = $response_json['meta']['total'];
$OFFSET = $OFFSET+100;
return $response_json['sales'];
} // end function.
// 各IDのデータをピックアップ
// details sale(id, account_id, name,postal, address1,addess2, )
function request_detail($context, $trxid){
$result = array();
$url = 'https://api.shop-pro.jp/v1/sales/' ;
$request = $trxid . '.json';
$response_body = file_get_contents($url.$request, false, $context);
$response_json = json_decode($response_body, true);
// データがない時のエラーハンドリング
$pos = strpos($http_response_header[0],'200');
if ($pos === false ){ //usually we don't see this message.
print_r($response_json);
return NULL;
}
$result['trxid'] = $trxid;
$result['make_date'] = date('Y-m-d',$response_json['sale']['make_date']);
$result['delivered_mail_sent_date'] = date('Y-m-d',$response_json['sale']['delivered_mail_sent_date']);
$result['product_total_price'] = $response_json['sale']['product_total_price'];
// echo " 注文:".date('Y-m-d H:i:s', $result['make_date']);
// echo " 発送メール:".date('Y-m-d H:i:s', $result['delivered_mail_sent_date']);
// echo " 金額:".$result['product_total_price'];
// トランザクション
$delivery = $response_json['sale']['sale_deliveries'][0];
unset($delivery['detail_ids']); // これは配列なのでうざいので消す
$result['customer_name'] = $delivery['name'];
// echo " 顧客名:". $result['customer_name']."<br>";
return $result;
} // end function.
//---- Main ----
header("Content-Type:text/html; charset=UTF-8"); // force chars UTF-8
header("Content-TYpe: application/force-download");
$context = set_up('あなた固有のトークン');
// Phase 1: get trxs.
$trxids = array();
while(true){
$salesdata = request_sales($context, $AFTER, $OFFSET);
foreach ($salesdata as $key => $row){
array_push($trxids, $row['id'] );
}
if ($TOTAL <= count($trxids)){
break;
}
}
// Phase 2: get detail.
$deliverydata = array();
$filename = "MC".$AFTER.".csv";
$fp = fopen($filename, "w");
$header = "トランザクションID, 受注日, 配送日,金額, 名前";
$sjis_header = mb_convert_encoding($header, 'SJIS-win', 'UTF-8');
@fwrite($fp, $sjis_header."\n");
foreach ($trxids as $trxid){
$deliverydata = request_detail($context, $trxid);
$csvdata = implode(",", $deliverydata);
// echo $csvdata."<BR>";
$sjis_csvdata = mb_convert_encoding($csvdata, 'SJIS-win', 'UTF-8');
@fwrite($fp, $sjis_csvdata."\n");
}
fclose($fp);
header('Content-Length: '. filesize($filename));
header('Content-disposition: attachment; filename="'.$filename.'"');
readfile($filename);
echo "DONE";
exit;

以上。

テストは自分のローカルのapacheで構いません。インターネットとつながっていればね。
言い換えるとデータは自分のローカルに入手できます。

関連記事

  1. XMLhttpRequest -> fetch

  2. Arduinoで脈拍を測る

  3. Visual Studio 2017 Visual Basic データ…

  4. P2Pは大事

  5. ソフトウェアは進歩し続けなけりゃならんのか?

  6. JQM テンプレートhtml

  7. Googleの検索結果をスプレッドシートに抜く方法

  8. CakePHP(3)

記事をプリント